package com.apes.hr.cultivate.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.cultivate.model.Course;
import com.apes.hr.cultivate.model.CourseLevel;
import com.apes.hr.cultivate.model.CultivatePlanAppraisal;
import com.apes.hr.cultivate.repository.CourseRepository;
import com.apes.hr.cultivate.repository.CultivatePlanAppraisalRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Set;

/**
 * 功能：培训课程
 *
 * @author Linwz
 * @create 2019-06-17 15:11
 */
@Service("hrCultivatePlanAppraisalService")
public class CultivatePlanAppraisalService extends DomainService {

    @Autowired
    CultivatePlanAppraisalRepository appraisalRepository;

    @Autowired
    CourseRepository courseRepository;
    /**
     * 设置成绩等级
     */
    private void setCourseLevel(CultivatePlanAppraisal appraisal){
        Course course = courseRepository.findOne(appraisal.getCourse().getId());
        Set<CourseLevel> courseLevelSet = course.getCourseLevelSet() ;
        appraisal.getCultivatePlanAppraisalItems().forEach(item ->{
            int success = item.getSuccess();
            courseLevelSet.forEach(courseLevel -> {
                if (success >= courseLevel.getValueL() && success<= courseLevel.getValueU()){
                    item.setCourseLevel(courseLevel.getCourseLevel());
                }
            });
        });
    }

    /**
     * 检查数据判断，判读当前的输入版本信息
     * @param request
     */
    public CultivatePlanAppraisal checkData(SimpleRequest request){
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        CultivatePlanAppraisal cultivatePlanAppraisal = appraisalRepository.findOne(id);
        if (cultivatePlanAppraisal == null) throw new RuntimeException("参训考核【" + id + "】在系统中不存在。");
        if (cultivatePlanAppraisal.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        return cultivatePlanAppraisal;
    }


    /***
     * 功能： 修改
     * @param request
     * @return
     */
    public CultivatePlanAppraisal update(SimpleRequest request) {
        CultivatePlanAppraisal oldBill = checkData(request);
        CultivatePlanAppraisal requestAppraisal = request.getO(CultivatePlanAppraisal.class);
        setCourseLevel(requestAppraisal);
        return appraisalRepository.saveAndFlush(requestAppraisal);

    }


    /**
     * 功能：审核培训计划安排
     *
     * @param request
     * @return
     */
    public CultivatePlanAppraisal approve(SimpleRequest request) {
        CultivatePlanAppraisal oldBill = checkData(request);
        if (oldBill.getApproveDate() != null) throw new RuntimeException("参训人员考核：" + request.get("id") + " 在系统中已审核。");
        CultivatePlanAppraisal ptType = request.getO(CultivatePlanAppraisal.class);
        ptType.setState("approve");
        ptType.setApproveDate(new Date());
        ptType.setApproveUid(this.getPerson(request));
        return appraisalRepository.saveAndFlush(ptType);
    }

    /***
     * 功能：回车查询
     * @param request
     * @return
     */
    public CultivatePlanAppraisal findOne(SimpleRequest request) {
        CultivatePlanAppraisal course = appraisalRepository.findOne((String) request.get("id"));
        return course;
    }

}
